import { categoryList } from './api';
import { companyList } from './api';

export const formItems = [
  // {
  //   label: '新闻',
  //   name: 'username',
  //   value: 'mingcheng',
  //   type: 'input',
  // },
  {
    label: '所属栏目页ID',
    name: 'category_id',
    required: true,
    type: 'select',
    asyncOptions: {
      api: categoryList,
      handleData(data) {
        return data.list.map((item) => {
          item.label = item.title;
          item.value = item.id;
          return item;
        });
      },
    },
  },
  {
    label: '所属企业',
    name: 'company_id',
    type: 'select',
    asyncOptions: {
      api: companyList,
      handleData(data) {
        return data.list.map((item) => {
          item.label = item.name;
          item.value = item.id;
          return item;
        });
      },
    },
  },
  {
    label: '新闻标题',
    name: 'title',
    type: 'input',
  },
  {
    label: '缩略图',
    name: 'pic',
    slot: 'pic',
    trigger: 'blur',
  },
  {
    label: '编写内容',
    name: 'contentType',
    value: {
      type: 0,
      content: '',
    },
    slot: 'contenttype',
  },
];

export const formItems2Rules = {
  // category_id: [
  //   {
  //     required: true,
  //     message: '请填写栏目ID',
  //   },
  // ],
  // company_id: [
  //   {
  //     type: 'number',
  //     required: true,
  //     message: '请选择所属企业',
  //   },
  // ],
  title: [
    {
      required: true,
      message: '请填写新闻标题',
    },
  ],
  pic: [
    {
      type: 'array',
      required: true,
      message: '请上传缩略图',
      trigger: 'blur',
    },
  ],
  contentType: [
    {
      type: 'object',
      required: true,
      trigger: 'change',
      validator: async (ruleObj, value) => {
        const urlExp =
          /^(((ht|f)tps?):\/\/)?[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/;

        if (value.type == 0 && !urlExp.test(value.content)) {
          return Promise.reject('链接格式不正确');
        }

        if (value.type == 1 && value.content.trim() == '') {
          return Promise.reject('请填写内容');
        }

        return Promise.resolve();
      },
    },
  ],
};

export const types = [
  {
    label: '外部链接',
    value: 0,
  },
  {
    label: '富文本编辑',
    value: 1,
  },
];
